package mya_dc.compilation_server.gui;

import java.sql.Timestamp;

import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import mya_dc.compilation_server.database.DatabaseManager;



public class ProjectCompilations implements TableModel 
{
	public ProjectCompilations(String sUserName, String sProjectName)
	{
		m_sUserName = sUserName;
		m_sProjectName = sProjectName;
		m_sCompilationTableQuery = "select * from COMPILATION where UserName like '" + m_sUserName + "' and ProjectName like '" + sProjectName + "'";
	}
	
	
	@Override
	public void addTableModelListener(TableModelListener l) {}

	
	@Override
	public Class<?> getColumnClass(int columnIndex) 
	{
		return m_sUserName.getClass();
	}

	
	@Override
	public int getColumnCount() 
	{
		return ColumnName.length;
	}
	
	
	@Override
	public String getColumnName(int columnIndex) 
	{
		return ColumnName[columnIndex];
	}

	@Override
	public int getRowCount() 
	{
		return getRowCountSync();
	}

	public synchronized int getRowCountSync() 
	{
		return DatabaseManager.countCompilations(m_sUserName, m_sProjectName);
	}
	
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) 
	{
		return getValueAtSync(rowIndex,columnIndex) ;
	}

	 
	public synchronized Object getValueAtSync(int rowIndex, int columnIndex) 
	{
		Timestamp CurrentTimestamp = (Timestamp)DatabaseManager.getCellContents(m_sCompilationTableQuery,rowIndex + 1,3);
		if (CurrentTimestamp == null)
		{
			return null;
		}
		String sCurrentTimestamp = CurrentTimestamp.toString();
		switch (columnIndex)
		{
			case 0: return sCurrentTimestamp.substring(0,10);
			case 1: return sCurrentTimestamp.substring(11);
		}
		return null;
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) 
	{
		return false;
	}

	@Override
	public void removeTableModelListener(TableModelListener l) {}

	
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {}
	
	
	private static String[] ColumnName = {"Date", "Time"};
	private String m_sUserName;
	private String m_sProjectName;
	private String m_sCompilationTableQuery;
}
	